package com.samphanie.dries.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2021/2/25 14:10
 */
@Data
@Entity
@Builder
@With
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "xx_users")
public class User implements UserDetails, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(length = 50, unique = true, nullable = false)
    private String username;
    @JsonIgnore
    @Column(length = 80, nullable = false)
    private String password;
    @Column(length = 11, unique = true, nullable = false)
    private String mobile;
    @Column(length = 255, unique = true, nullable = false)
    private String email;
    @Column(length = 50)
    private String nickname;
    @Column(nullable = false)
    private boolean enabled;
    @Column(nullable = false)
    private boolean accountNonExpired;
    @Column(nullable = false)
    private boolean accountNonLocked;
    @Column(nullable = false)
    private boolean credentialsNonExpired;

    /**
     * 是否启用两步验证
     */
    @Builder.Default
    @Column(nullable = false)
    private boolean usingMfa = false;
    /**
     * 两步验证的 key
     */
    @JsonIgnore
    @Column(nullable = false)
    private String mfaKey;

    @ManyToMany
    @Fetch(FetchMode.JOIN)
    @JoinTable(name = "xx_users_roles",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}
    )
    private Set<Role> authorities;

}
